import UserService from '@/service/UserService';
import Util from '@/utils/Util';
import Constant, { TopBar } from '@/constant/Constants';
import LoggerUtil from '@/utils/LoggerUtil';
import UniUtil, { VideoAdOptions } from '@/utils/UniUtil';
import CommonService from '@/service/CommonService';
import { UserRequest } from '@/protocol/request/UserRequest';
import AdService from '@/service/AdService';
import { AdRequest } from '@/protocol/request/AdRequest';
import { AdResponse } from '@/protocol/response/AdResponse';
import { ConfigureResponse, QrcodeConfig } from '@/protocol/response/CommonResponse';
import { UserInfo } from "@/model/UserInfo";
import { MyVue } from "@/common/MyVue";
import { Component } from 'vue-facing-decorator';

@Component
export default class Score extends MyVue {
    showLoginModal: boolean = false;
    user = new UserInfo.User();
    adConfig = new AdResponse.AdConfig();
    ad = null;
    adOptions = new VideoAdOptions();
    showModal: boolean = false;
    configure = new ConfigureResponse();
    onLoad() {
        uni.setNavigationBarTitle({ title: TopBar.USER_SCORE });
        this.adOptions.id = Constant.adId;
        this.ad = UniUtil.createVideoAd(this.adOptions);
    }

    async loadPost() {
        if (this.showModal) {
            this.showModal = false;
            return;
        }
        if (!UserService.checkAuth()) {
            return;
        }
        let user = UserService.getLoginResponse();
        Util.copy(this.user, user);
        let data = await UserService.refreshUserData();
        Util.copy(this.user, data);
        let adConfigure = await AdService.config();
        Util.copy(this.adConfig, adConfigure);
        let configure = await CommonService.getCongirue();
        Util.copy(this.configure, configure);
    }

    onToInvite() {
        UniUtil.navigateTo("/pages/invite/invite");
    }

    onToHome() {
        //UniUtil.navigateBack();
        uni.switchTab({ url: "/pages/home/home" });
    }
    async onSignIn() {
        let success = UserService.checkAuth();
        if (!success) {
            return;
        }
        if (this.user.signIn) {
            this.showLoginModal = true;
        } else {
            let that = this;
            let res = await UserService.signin();
            if (null != res) {
                let user = await UserService.refreshUserData();
                Util.copy(this.user, user);
            }
        }
    }

    onCloseLoginModal() {
        this.showLoginModal = false;
    }

    onToActivityWeb() {
        let success = UserService.checkAuth();
        if (success) {
            UniUtil.navigateTo("/pages/activity/activity");
        }
    }

    onToScoreList() {
        UniUtil.navigateTo("/pages/score/score-list");
    }

    onToLookAd() {
        let that = this;
        if (this.adConfig.leftTimes <= 0) {
            UniUtil.showError({ title: "无可用次数" });
            return;
        }
        let now = new Date().getTime();
        if (now < this.adConfig.nextStartTime) {
            let leftTimes = ((this.adConfig.nextStartTime - now) / 1000).toFixed(0);
            UniUtil.showWarn({ title: "还有" + leftTimes + "秒" });
            return;
        }
        this.adOptions.success = async () => {
            let request = new AdRequest.Look();
            let res = await AdService.look(request);
            if (Util.isEmpty(res)) {
                return;
            }
            UniUtil.showToast({ title: "获得" + res.addScore + "积分" });
            that.adConfig.leftTimes = res.leftTimes;
            that.adConfig.nextStartTime = res.nextStartTime;
            that.user.score += res.addScore;
        }
        that.ad.show();
        this.showModal = true;

    }

    onToMyGroup() {
        let success = UserService.checkAuth();
        if (success) {
            UniUtil.navigateTo("/pages/user/user-qrcode");

        }
    }

    onToRechargeScore() {
        let recharge = <any>this.$refs.Recharge;
        recharge.onShowModal();
    }



}